package chessboard;

import java.awt.Color;

public class ChessBoard {

	Field[][] fields;

	public ChessBoard() {
		this.fields = new Field[12][8];
		init();
	}

	public void init(){
		for(int i = 0; i < 12; i++){
			for(int j = 0; j < 8; j++){
				Color c = Color.GRAY;
				if(j > 5 && i < 9 && i > 0){
					c = Color.BLACK;
				} else if(j < 2 && i < 9 && i > 0){
					c = Color.WHITE;
				}
				ChessMan man = new ChessMan(Type.NONE, c);
				if(i != 0 && i < 9 && (j == 1 || j == 6)){
					man = new ChessMan(Type.PAWN, c);
				} else if(i != 0 && i != 9 && (j == 0 || j == 7)){
					if(i == 1 || i == 8){
						man = new ChessMan(Type.ROOK, c);						
					} else if(i == 2 || i == 7){
						man = new ChessMan(Type.KNIGHT, c);						
					} else if(i == 3 || i == 6){
						man = new ChessMan(Type.BISHOP, c);						
					} else if(i == 4){
						man = new ChessMan(Type.QUEEN, c);							
					} else if(i == 5){
						man = new ChessMan(Type.KING, c);	
					}
				}
				if(i == 0){
					fields[i][j] = new Field('0', j+1, man);					
				} else if(i == 9){
					fields[i][j] = new Field('1', j+1, man);					
				} else if(i == 10){
					fields[i][j] = new Field('2', j+1, man);					
				}else if(i == 11){
					fields[i][j] = new Field('3', j+1, man);					
				}else {
					fields[i][j] = new Field((char)(i+64), j+1, man);
				}
			}
		}
	}
	
	public void empty(){
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < 8; j++){
				get(i,j).setChessMan(new ChessMan(Type.NONE, Color.GRAY));
			}
		}
	}
	
	public Field[][] getFields() {
		return fields;
	}
	
	public Field get(char a, int i){
		int aa;
		if(!Character.isDigit(a)){
			aa =(int)Character.toLowerCase(a) -96;
		} else {
			aa = (int)a - 48;
			if(aa > 0){
				aa += 8;
			}
		}
//		System.out.println("got: " + aa + String.valueOf(i-1));
		return fields[aa][i-1];
	}
	
	public Field get(int i, int j){
		return fields[i][j];
	}

	public void output(){
		System.out.println("  |    1   |    2  |    3   |    4   |    5   |    6   |    7   |    8   |");
		for(int i = 0; i < 12; i++){
			if(i < 1 || i > 8){
				System.out.print(i + " | ");
			} else {
				System.out.print((char)(i+96) + " | ");				
			}
			for(int j = 0; j < 8; j++){
				System.out.print(get(i,j).getChessMan() + ", ");
			}
			System.out.println();
		}
		System.out.println("  |    1   |    2  |    3   |    4   |    5   |    6   |    7   |    8   |");
	}
	
}
